package com.putable.nmc.world;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;


import junit.framework.TestCase;

public class TestWorld extends TestCase {

    public static void main(String[] args) {
        junit.textui.TestRunner.run(TestWorld.class);
    }

    protected void setUp() throws Exception {
        super.setUp();
    }
    private String[] saveWorld(World w) {
        StringWriter staffWriter = new StringWriter();
        StringWriter worldWriter = new StringWriter();
        try {
            w.save(staffWriter,worldWriter); 
            staffWriter.close();
            worldWriter.close();
        } 
        catch (IOException e) {
            fail("Exception thrown "+ e);
        }
        return new String[] { staffWriter.toString(), worldWriter.toString() };
    }

    public void testWorldSaveLoad() {
        World w = new World();
        String[] saved = saveWorld(w);
        {
            Reader readStaff = new StringReader(saved[0]);
            Reader readWorld = new StringReader(saved[1]);
            World w2 = new World(readStaff,readWorld);
            String[] sw2 = saveWorld(w2);
            assertEquals(saved[0],sw2[0]);
            assertEquals(saved[1],sw2[1]);
        }
    }

    public void testNextCustomer() {
        World w = new World();
        double lastNow = 0;
        double newNow = 0;
        while (true) {
            CIT cust = w.getNextCIT(lastNow);
            if (cust == null) break;
            newNow = cust.getArrivalTime();
            assertTrue(newNow >= lastNow);
            lastNow = newNow;
        }
    }
}
